package com.umfb.edu;

import com.qiniu.util.Auth;
import com.qiniu.util.Base64;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlSafeBase64;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by zcah on 18-7-20.
 */
public class put64 {

    String ak = "yU1eCAiHArlu01RWGSoXLSMuJ6zK80_JTjMclP8f";
    String sk = "zFmEBtTqQTog9L4nFFx13HPOQfgXfUy-bx2MnsyV";    // 密钥配置
    Auth auth = Auth.create(ak, sk);    // TODO Auto-generated constructor stub
    String bucketname = "umfb-bucket";    //空间名
    String key = "00000000000000000000";    //上传的图片名

    public String generateKey() {
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHss");
        String current = format.format(now);
        StringBuilder sb = new StringBuilder(current);
        String rand = RandomStringUtils.random(4, "0123456789");
        Random random = new Random(now.getTime());
        int idx1 = random.nextInt(9);
        sb.insert(idx1, rand.substring(0, 2));
        int idx2 = random.nextInt(9);
        sb.insert(idx2, rand.substring(2));
        sb.insert(sb.length() - 2, rand.substring(1, 3));
        return sb.toString();
    }

    public String getUpToken() {
        return auth.uploadToken(bucketname, generateKey(), 3600, new StringMap().put("insertOnly", 1));
    }
    public void put64image(String filePath) throws Exception {
        String file = "/home/zcah/cheng.gif";//图片路径
        FileInputStream fis = null;
        int l = (int) (new File(file).length());
        byte[] src = new byte[l];
        fis = new FileInputStream(new File(filePath));
        fis.read(src);
        String file64 = Base64.encodeToString(src, 0);
        String url = "http://upload.qiniup.com/putb64/" + l+"/key/"+ UrlSafeBase64.encodeToString(generateKey());
        //非华东空间需要根据注意事项 1 修改上传域名
        RequestBody rb = RequestBody.create(null, file64);
        Request request = new Request.Builder().
                url(url).
                addHeader("Content-Type", "application/octet-stream")
                .addHeader("Authorization", "UpToken " + getUpToken())
                .post(rb).build();
        System.out.println(request.headers());
        OkHttpClient client = new OkHttpClient();
        okhttp3.Response response = client.newCall(request).execute();
        System.out.println(response);
    }

    @Test
    public void traverseFolder2() throws  Exception{
        String path = "/home/zcah/hanyu";
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                    } else {
                        put64image(file2.getAbsolutePath());

                        System.out.println("文件:" + file2.getAbsolutePath());
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }
    public static void main(String[] args) throws Exception {
//        new put64().put64image();
    }

}
